<?php

namespace Wechat\Controller;

class WorkorderController extends CommonController
{
    function getImageUrl() {
        $imgData= I("post.imgData");

        if (!isNullOrEmpty($imgData)) {
            echo json_encode($this->uploadImage($imgData));
        }
    }

	
    public function index()
    {

        if(!check_role($this->customer["customer_id"], "1,2,3,5,6,7,8,9,10")){
            $this->error("您没有访问权限！");
            exit();
        }

        $id = $_REQUEST["id"];
        $workorder = D("Workorder")->alias("wo")
            ->join("left join __CLIENT_INFO__ ci on ci.client_info_id = wo.client_info_id")
            ->where("wo.workorder_id=$id")
            ->field("wo.*,ci.client_info_name")
            ->find();

        $patientInfo = D("Patient")->queryById($workorder["patient_id"]);
        if($workorder["workorder_type"] == 1){
            $examBasicInfo = D("ExamBasic")->queryById($workorder["exam_basic_id"]);
            $examBasicCPInfo = D("ExamBasicCp")->queryById($workorder["exam_basic_cp_id"]);
            $examMeasureInfo = D("ExamMeasure")->queryById($workorder["exam_measure_id"]);
            $rxInfo = D("Rx")->queryById($workorder["rx_id"]);
        }
        if($workorder["workorder_type"] == 2){
            $examBasicInfo = D("ExamBasicSpine")->queryById($workorder["exam_basic_spine_id"]);
            $examMeasureInfo = D("ExamMeasureSpine")->queryById($workorder["exam_measure_spine_id"]);
        }
        if($workorder["workorder_type"] == 3){
            $examBasicCPInfo = D("ExamBasicCpPlus")->queryById($workorder["exam_basic_cp_id"]);
        }

        $examAnalysisInfo = D("ExamAnalysis")->queryById($workorder["exam_analysis_id"]);
        $sales = D("admin")->where(array('employee_id'=>$workorder["sales_id"]))->find();
        $prescriber = D("admin")->where(array('employee_id'=>$workorder["prescriber_id"]))->find();
        if($workorder["examinator_id"] == -1){
            $examinator["admin_name"] = "无";
        }else{
            $examinator = D("admin")->where(array('employee_id'=>$workorder["examinator_id"]))->find();
        }
        $map['class_name'] = array('like', array('all', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $creator = D("admin")->where(array('employee_id'=>$workorder["creator_id"]))->find();

        $listClientInfo = D('ClientInfo')->where(array('enterprise_code'=>$user["enterprise_code"]))->select();
        $this->assign("listClientInfo", $listClientInfo);
        $this->assign("user", $user);
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("patientInfo", $patientInfo);
        $this->assign("examBasicInfo", $examBasicInfo);
        $this->assign("examBasicCPInfo", $examBasicCPInfo);
        $this->assign("examMeasureInfo", $examMeasureInfo);
        $this->assign("rxInfo", $rxInfo);
        $this->assign("examAnalysisInfo", $examAnalysisInfo);
        $this->assign("formDict", $formDict);
        $this->assign("sales", $sales);
        $this->assign("examinator", $examinator);
        $this->assign("prescriber", $prescriber);
        $this->assign("creator", $creator);
        $this->display("index");

    }

    public function patientInfo()
    {
        $patientId = $_REQUEST["patientId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($patientId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $patientId = $workorder["patient_id"];
        }

        $patientInfo = D("Patient")->queryById($patientId);
        $employee = D("admin")->where(array('employee_id'=>$patientInfo["update_id"]))->find();
        $map["class_name"] = array('like', array('patient', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("patientInfo", $patientInfo);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("patientId", $patientId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("patientInfo");

    }

    public function checkPatientInfo()
    {
        $map["patient_name"] = I("post.name");
//        $map["id_no"] = I("post.idNo");
        $patientInfo = D("Patient")->queryForList(null,$map);

        if (isNullOrEmpty($patientInfo)) {
            $data["code"] = 0;
        } else {
            $data = $patientInfo;
        }
        echo json_encode($data);

    }

    public function getPatientInfo()
    {
        $map["patient_id"] = I("post.patientId");
        $patientInfo = D("Patient")->queryByMap($map);

        if (isNullOrEmpty($patientInfo)) {
            $data["code"] = 0;
        } else {
            $data = $patientInfo;
            $data["code"] = 200;
        }
        echo json_encode($data);
    }

    public function examBasic()
    {

        $examBasicId = $_REQUEST["examBasicId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examBasicId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examBasicId = $workorder["exam_basic_id"];
        }
        $examBasic = D("ExamBasic")->queryById($examBasicId);

        $employee = D("admin")->where(array('employee_id'=>$examBasic["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_basic', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examBasic", $examBasic);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examBasicId", $examBasicId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        if($examBasic["version"] == 1){
            $this->display("examBasic");
        }else{
            $this->display("examBasicv2");
        }
    }

    public function examMeasure()
    {

        $examMeasureId = $_REQUEST["examMeasureId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examMeasureId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examMeasureId = $workorder["exam_measure_id"];
        }

        $examMeasure = D("ExamMeasure")->queryById($examMeasureId);
        $employee = D("admin")->where(array('employee_id'=>$examMeasure["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_measure', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examMeasure", $examMeasure);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examMeasureId", $examMeasureId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        if($examMeasure["version"] == 1){
            $this->display("examMeasure");
        }else{
            $this->display("examMeasurev2");
        }

    }

    public function examBasicCP()
    {
        $examBasicCPId = $_REQUEST["examBasicCPId"];
        $workorderId = $_REQUEST["workorderId"];
        $workorder = D("Workorder")->queryById($workorderId);

        if (isNullOrEmpty($examBasicCPId)) {
            $examBasicCPId = $workorder["exam_basic_cp_id"];
        }
        if($workorder["workorder_type"] == 3){
            $examBasicCP = D("ExamBasicCpPlus")->queryById($examBasicCPId);
        }else{
            $examBasicCP = D("ExamBasicCp")->queryById($examBasicCPId);
        }

        $employee = D("admin")->where(array('employee_id'=>$examBasicCP["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_basic_cp', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examBasicCP", $examBasicCP);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examBasicCPId", $examBasicCPId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        if($workorder["workorder_type"] == 3){
            $this->display("examBasicCPPlus");
        }else{
            $this->display("examBasicCP");
        }


    }

    public function examAnalysis()
    {

        $examAnalysisId = $_REQUEST["examAnalysisId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examAnalysisId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examAnalysisId = $workorder["exam_analysis_id"];
        }

        $examAnalysis = D("ExamAnalysis")->queryById($examAnalysisId);
        $employee = D("admin")->where(array('employee_id'=>$examAnalysis["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_analysis', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("examAnalysis", $examAnalysis);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examAnalysisId", $examAnalysisId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examAnalysis");

    }

    public function examBasicSpine()
    {

        $examBasicSpineId = $_REQUEST["examBasicSpineId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examBasicSpineId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examBasicSpineId = $workorder["exam_basic_spine_id"];
        }
        $examBasicSpine = D("ExamBasicSpine")->queryById($examBasicSpineId);

//        $examBasic = D("ExamBasic")->alias("eb")
//            ->join("left join __FORMDICT__ fd on fd.group_id = gm.group_id")
//            ->join("left join __CUSTOMER__ c on c.customer_id = gm.customer_id")
//            ->where($failureGroupCustomerMap)
//            ->field("c.customer_id, c.openid")
//            ->select();


        $employee = D("admin")->where(array('employee_id'=>$examBasicSpine["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_basic_spine', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examBasicSpine", $examBasicSpine);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examBasicSpineId", $examBasicSpineId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examBasicSpine");

    }

    public function examBasicSco()
    {

        $examBasicId = $_REQUEST["examBasicId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examBasicId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examBasicId = $workorder["exam_basic_id"];
        }
        $examBasic = D("ExamBasicCommon")->queryById($examBasicId);

        $employee = D("admin")->where(array('employee_id'=>$examBasic["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_basic_common', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examBasic", $examBasic);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examBasicId", $examBasicId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examBasicSco");

    }

    public function examBasicKafo()
    {
        $examBasicId = $_REQUEST["examBasicId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examBasicId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examBasicId = $workorder["exam_basic_id"];
        }
        $examBasic = D("ExamBasicCommon")->queryById($examBasicId);

        $employee = D("admin")->where(array('employee_id'=>$examBasic["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_basic_common', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examBasic", $examBasic);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examBasicId", $examBasicId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examBasicKafo");
    }

    public function examBasicThigh()
    {
        $examBasicId = $_REQUEST["examBasicId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examBasicId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examBasicId = $workorder["exam_basic_id"];
        }
        $examBasic = D("ExamBasicCommon")->queryById($examBasicId);

        $employee = D("admin")->where(array('employee_id'=>$examBasic["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_basic_common', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examBasic", $examBasic);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examBasicId", $examBasicId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examBasicThigh");
    }

    public function examMeasureThigh()
    {

        $examMeasureId = $_REQUEST["examMeasureId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examMeasureId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examMeasureId = $workorder["exam_measure_id"];
        }

        $examMeasure = D("ExamMeasureCommon")->queryById($examMeasureId);
        $employee = D("admin")->where(array('employee_id'=>$examMeasure["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_measure', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examMeasure", $examMeasure);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examMeasureId", $examMeasureId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examMeasureThigh");
    }

    public function examBasicThighPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examBasicId = I("post.examBasicId");
        if (isNullOrEmpty($examBasicId)) {
            $examBasicId = D('ExamBasicCommon')->add(array(
                'completed' => -1,
                'version' => 2,
                'type' => $workorder["workorder_type"],
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examBasicInfo = D("ExamBasicCommon")->queryById($examBasicId);

        $examBasicInfo["update_time"] = getCurrentDate();
        $examBasicInfo["update_id"] = $this->customer["customer_id"];
        $examBasicInfo["completed"] = I("post.completed");

        $examBasicInfo["side"] = I("post.side");
        $workorder["side"] = I("post.side");
        for($i = 0; $i <= 41; $i++){
            $examBasicInfo["eb".$i] = I("post.eb".$i);
        }

        $examBasicInfo["ebl0"] = I("post.ebl0");

        D("ExamBasicCommon")->updateInfo($examBasicInfo);
        $workorder["exam_basic_id"] = $examBasicId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = $examBasicId;
        echo json_encode($data);
    }

    public function examMeasureThighPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examMeasureId = I("post.examMeasureId");
        if (isNullOrEmpty($examMeasureId)) {
            $examMeasureId = D('ExamMeasureCommon')->add(array(
                'completed' => -1,
                'version' => 2,
                'type' => $workorder["workorder_type"],
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examMeasureInfo = D("ExamMeasureCommon")->queryById($examMeasureId);

        $examMeasureInfo["update_time"] = getCurrentDate();
        $examMeasureInfo["update_id"] = $this->customer["customer_id"];
        $examMeasureInfo["completed"] = I("post.completed");

        $examMeasureInfo["burden"] = I("post.burden");
        for($i = 0; $i <= 24; $i++){
            $examMeasureInfo["em".$i] = I("post.em".$i);
        }

        D("ExamMeasureCommon")->updateInfo($examMeasureInfo);
        $workorder["exam_measure_id"] = $examMeasureId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;

        echo json_encode($data);
    }

    public function examBasicShank()
    {
        $examBasicId = $_REQUEST["examBasicId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examBasicId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examBasicId = $workorder["exam_basic_id"];
        }
        $examBasic = D("ExamBasicCommon")->queryById($examBasicId);

        $employee = D("admin")->where(array('employee_id'=>$examBasic["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_basic_common', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examBasic", $examBasic);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examBasicId", $examBasicId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examBasicShank");
    }

    public function examMeasureShank()
    {

        $examMeasureId = $_REQUEST["examMeasureId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examMeasureId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examMeasureId = $workorder["exam_measure_id"];
        }

        $examMeasure = D("ExamMeasureCommon")->queryById($examMeasureId);
        $employee = D("admin")->where(array('employee_id'=>$examMeasure["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_measure', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examMeasure", $examMeasure);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examMeasureId", $examMeasureId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examMeasureShank");
    }

    public function examBasicShankPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examBasicId = I("post.examBasicId");
        if (isNullOrEmpty($examBasicId)) {
            $examBasicId = D('ExamBasicCommon')->add(array(
                'completed' => -1,
                'version' => 2,
                'type' => $workorder["workorder_type"],
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examBasicInfo = D("ExamBasicCommon")->queryById($examBasicId);

        $examBasicInfo["update_time"] = getCurrentDate();
        $examBasicInfo["update_id"] = $this->customer["customer_id"];
        $examBasicInfo["completed"] = I("post.completed");

        $examBasicInfo["side"] = I("post.side");
        $workorder["side"] = I("post.side");
        for($i = 0; $i <= 41; $i++){
            $examBasicInfo["eb".$i] = I("post.eb".$i);
        }

        $examBasicInfo["ebl0"] = I("post.ebl0");

        D("ExamBasicCommon")->updateInfo($examBasicInfo);
        $workorder["exam_basic_id"] = $examBasicId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = $examBasicId;
        echo json_encode($data);
    }

    public function examMeasureShankPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examMeasureId = I("post.examMeasureId");
        if (isNullOrEmpty($examMeasureId)) {
            $examMeasureId = D('ExamMeasureCommon')->add(array(
                'completed' => -1,
                'version' => 2,
                'type' => $workorder["workorder_type"],
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examMeasureInfo = D("ExamMeasureCommon")->queryById($examMeasureId);

        $examMeasureInfo["update_time"] = getCurrentDate();
        $examMeasureInfo["update_id"] = $this->customer["customer_id"];
        $examMeasureInfo["completed"] = I("post.completed");

        $examMeasureInfo["burden"] = I("post.burden");
        for($i = 0; $i <= 24; $i++){
            $examMeasureInfo["em".$i] = I("post.em".$i);
        }

        D("ExamMeasureCommon")->updateInfo($examMeasureInfo);
        $workorder["exam_measure_id"] = $examMeasureId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;

        echo json_encode($data);
    }

    public function examMeasureSpine()
    {
//        if (!check_access($this->customer["customer_id"], "0", 100)) {
//            $this->error("您没有访问权限！");
//            exit();
//        }

        $examMeasureSpineId = $_REQUEST["examMeasureSpineId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examMeasureSpineId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examMeasureSpineId = $workorder["exam_measure_spine_id"];
        }

        $examMeasureSpine = D("ExamMeasureSpine")->queryById($examMeasureSpineId);
        $employee = D("admin")->where(array('employee_id'=>$examMeasureSpine["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_measure_spine', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examMeasureSpine", $examMeasureSpine);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examMeasureSpineId", $examMeasureSpineId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        if($examMeasureSpine["version"] == 1){
            $this->display("examMeasureSpine");
        }else{
            $this->display("examMeasureSpinev2");
        }
    }

    public function examMeasureSco()
    {
//        if (!check_access($this->customer["customer_id"], "0", 100)) {
//            $this->error("您没有访问权限！");
//            exit();
//        }

        $examMeasureId = $_REQUEST["examMeasureId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examMeasureId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examMeasureId = $workorder["exam_measure_id"];
        }

        $examMeasure = D("ExamMeasureCommon")->queryById($examMeasureId);
        $employee = D("admin")->where(array('employee_id'=>$examMeasure["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_measure_common', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examMeasure", $examMeasure);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examMeasureId", $examMeasureId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examMeasureSco");
    }

    public function examMeasureSpineDraw()
    {
//        if (!check_access($this->customer["customer_id"], "0", 100)) {
//            $this->error("您没有访问权限！");
//            exit();
//        }

        $examMeasureSpineId = $_REQUEST["examMeasureSpineId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examMeasureSpineId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examMeasureSpineId = $workorder["exam_measure_spine_id"];
        }

        $examMeasureSpine = D("ExamMeasureSpine")->queryById($examMeasureSpineId);
        $employee = D("admin")->where(array('employee_id'=>$examMeasureSpine["update_id2"]))->find();
        $map['class_name'] = array('like', array('exam_measure', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examMeasureSpine", $examMeasureSpine);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examMeasureSpineId", $examMeasureSpineId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examMeasureSpineDraw");

    }

    public function examMeasureKafo()
    {

        $examMeasureId = $_REQUEST["examMeasureId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examMeasureId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examMeasureId = $workorder["exam_measure_id"];
        }

        $examMeasure = D("ExamMeasureCommon")->queryById($examMeasureId);
        $employee = D("admin")->where(array('employee_id'=>$examMeasure["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_measure', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examMeasure", $examMeasure);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examMeasureId", $examMeasureId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examMeasureKafo");
    }

    public function workorderList()
    {
        $status = I("post.status");
        $startdate = I("post.startdate");
        $enddate = I("post.enddate");
        $map["wo.workorder_type"] = I("post.workorderType");
        if (!isNullOrEmpty($status)) {
            $map["wo.status"] = $status;
        }

        $employee = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $role = explode(',',$employee["role"]);

        $where["wo.creator_id"] = $this->customer["customer_id"];
        if ($status == -1) {
//            $map2["wo.creator_id"] = $this->customer["customer_id"];
        }


        if ($status == 1) {
            if (in_role($role,"5")) {
                $all = true;
            } else {
                $where["wo.sales_id"] = $this->customer["customer_id"];
                $where['_logic'] = 'or';
            }
        }
        if ($status == 2) {
            if (in_role($role,"3,6,7,8,9,10")) {
                $all = true;
            } else {
                $where["wo.sales_id"] = $this->customer["customer_id"];
                $where["wo.examinator_id"] = $this->customer["customer_id"];
                $where['_logic'] = 'or';
            }
//        $where['title']  = array('like','%thinkphp%');

        }
        if ($status == 3 || $status == 4) {
            if (in_role($role,"3,5,6,7,8,9,10")) {
                $all = true;
            } else {
                $where["wo.sales_id"] = $this->customer["customer_id"];
                $where["wo.examinator_id"] = $this->customer["customer_id"];
                $where['_logic'] = 'or';
            }
//        $where['title']  = array('like','%thinkphp%');

        }
        if ($status == 0) {
            if (in_role($role,"3,5")) {
                $all = true;
            } else {
                $where["wo.sales_id"] = $this->customer["customer_id"];
                $where["wo.examinator_id"] = $this->customer["customer_id"];
                $where['_logic'] = 'or';
            }
//        $where['title']  = array('like','%thinkphp%');

        }
        if (in_role($role,"1")) {
            $all = true;
        }else{
            $map["wo.enterprise_code"] = $employee["enterprise_code"];
        }
        if ($all != true){
            $map2["_complex"] = $where;
        }

//        if($status == 1){
//            $map2["wo.creator_id"] = $this->customer["customer_id"];
//        }else{
//            if ($job[4] == 1) {
//                $map2["wo.sales_id"] = $this->customer["customer_id"];
////              $map2['_logic'] = 'OR';
//            } else {
//                $map2 = null;
//            }
//        }

        if (!isNullOrEmpty($startdate)) {
            $map["wo#create_time-egt"] = $startdate;
        }
        if (!isNullOrEmpty($enddate)) {
            $map["wo#create_time-elt"] = $enddate . " 23:59:59";
        }
//
//        $list = D("Order")->alias("o")
//            ->join("left join __PRODUCT__ p on p.product_id = o.product_id")
//            ->where($map)
//            ->order("o.order_id desc")
//            ->field("o.*,p.price as single_price,p.image_url,p.product_name")
//            ->select();

        $workOrderList = D("Workorder")->queryForList(null, $map, $map2);
        echo json_encode($workOrderList);
    }

    /**
     * 保存患者基本资料
     */
    public function patientInfoPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $patientId = I("post.patientId");
        if (isNullOrEmpty($patientId)) {
            $patientId = D('Patient')->add(array(
                'completed' => -1,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $patientInfo = D("Patient")->queryById($patientId);

        $patientInfo["patient_name"] = I("post.name");
        $patientInfo["sex"] = I("post.sex");
        $patientInfo["age"] = I("post.age");
        $patientInfo["height"] = I("post.height");
        $patientInfo["heightsit"] = I("post.heightsit");
        $patientInfo["weight"] = I("post.weight");
        $patientInfo["diagnosis"] = I("post.diagnosis");
        $patientInfo["happen_date"] = I("post.happenDate");
        $patientInfo["id_no"] = I("post.idNo");
        $patientInfo["address"] = I("post.address");
        $patientInfo["contact"] = I("post.contact");
        $patientInfo["mobile"] = I("post.mobile");
        $patientInfo["hospital"] = I("post.hospital");
        $patientInfo["doctor"] = I("post.doctor");

        $patientInfo["reason"] = I("post.reason");
        $patientInfo["compl"] = I("post.compl");
        $patientInfo["allergy"] = I("post.allergy");
        $patientInfo["old_orth"] = I("post.oldOrth");
        $patientInfo["orth_part"] = I("post.orthPart");
        $patientInfo["history"] = I("post.history");
        $patientInfo["diagnosis1"] = I("post.diagnosis1");
        $patientInfo["diagnosis2"] = I("post.diagnosis2");
        $patientInfo["diagnosis3"] = I("post.diagnosis3");
        $patientInfo["diagnosis4"] = I("post.diagnosis4");
        $patientInfo["diagnosis5"] = I("post.diagnosis5");
        $patientInfo["special"] = I("post.special");
        $patientInfo["update_time"] = getCurrentDate();
        $patientInfo["update_id"] = $this->customer["customer_id"];
        $patientInfo["completed"] = I("post.completed");
        D("Patient")->updateInfo($patientInfo);

        $workorder["patient_id"] = $patientId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
        echo json_encode($data);
    }

    public function examBasicPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examBasicId = I("post.examBasicId");
        if (isNullOrEmpty($examBasicId)) {
            $examBasicId = D('ExamBasic')->add(array(
                'completed' => -1,
                'version' => 2,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examBasicInfo = D("ExamBasic")->queryById($examBasicId);

        $examBasicInfo["update_time"] = getCurrentDate();
        $examBasicInfo["update_id"] = $this->customer["customer_id"];
        $examBasicInfo["completed"] = I("post.completed");
        $examBasicInfo["version"] = 2;

        $examBasicInfo["side"] = I("side");
        $workorder["side"] = I("side");
        $examBasicInfo["mobility"] = I("post.mobility");
        $examBasicInfo["shankcontrol"] = I("post.shankcontrol");
        $examBasicInfo["gait"] = I("post.gait");
        $examBasicInfo["notegait"] = I("post.notegait");
        $examBasicInfo["footarch"] = I("post.footarch");
        $examBasicInfo["notefootarch"] = I("post.notefootarch");
        $examBasicInfo["toe"] = I("post.toe");
        $examBasicInfo["notetoe"] = I("post.notetoe");
        $examBasicInfo["forefoot"] = I("post.forefoot");
        $examBasicInfo["complforefoot"] = I("post.complforefoot");
        $examBasicInfo["noteforefoot"] = I("post.noteforefoot");
        $examBasicInfo["heela"] = I("post.heela");
        $examBasicInfo["heelb"] = I("post.heelb");
        $examBasicInfo["heelaangle"] = I("post.heelaangle");
        $examBasicInfo["heelbangle"] = I("post.heelbangle");
        $examBasicInfo["complheel"] = I("post.complheel");
        $examBasicInfo["noteheel"] = I("post.noteheel");
        $examBasicInfo["height_heel"] = I("post.heightHeel");
        $examBasicInfo["shoe_type"] = I("post.shoeType");
        $examBasicInfo["shoe_size"] = I("post.shoeSize");
        $examBasicInfo["burden"] = I("post.burden");
        $examBasicInfo["burdenfoot"] = I("post.burdenfoot");
        $examBasicInfo["discomfort"] = I("post.discomfort");
        $examBasicInfo["anklesymmetry"] = I("post.anklesymmetry");
        $examBasicInfo["dysfunction"] = I("post.dysfunction");
        $examBasicInfo["takephoto"] = I("post.takephoto");
        $examBasicInfo["takevideo"] = I("post.takevideo");

        D("ExamBasic")->updateInfo($examBasicInfo);
        $workorder["exam_basic_id"] = $examBasicId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "$examBasicId".$examBasicId;
        echo json_encode($data);
    }

    public function examMeasurePost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examMeasureId = I("post.examMeasureId");
        if (isNullOrEmpty($examMeasureId)) {
            $examMeasureId = D('ExamMeasure')->add(array(
                'completed' => -1,
                'version' => 2,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examMeasureInfo = D("ExamMeasure")->queryById($examMeasureId);

        $examMeasureInfo["update_time"] = getCurrentDate();
        $examMeasureInfo["update_id"] = $this->customer["customer_id"];
        $examMeasureInfo["completed"] = I("post.completed");
        $examMeasureInfo["version"] = 2;

        $examMeasureInfo["burden"] = I("post.burden");
        $examMeasureInfo["shank_0"] = I("post.shank0");
        $examMeasureInfo["shank_1"] = I("post.shank1");
        $examMeasureInfo["shank_2"] = I("post.shank2");
        $examMeasureInfo["shank_3"] = I("post.shank3");
        $examMeasureInfo["shank_4"] = I("post.shank4");
        $examMeasureInfo["shank_5"] = I("post.shank5");
        $examMeasureInfo["shank_6"] = I("post.shank6");
        $examMeasureInfo["shank_7"] = I("post.shank7");
        $examMeasureInfo["shank_8"] = I("post.shank8");
        $examMeasureInfo["shank_9"] = I("post.shank9");
        $examMeasureInfo["shank_10"] = I("post.shank10");
        $examMeasureInfo["shank_11"] = I("post.shank11");
        $examMeasureInfo["shank_12"] = I("post.shank12");
        $examMeasureInfo["shank_13"] = I("post.shank13");
        $examMeasureInfo["shank_14"] = I("post.shank14");
        $examMeasureInfo["shank_15"] = I("post.shank15");
//        $examMeasureInfo["shank_16"] = I("post.shank16");
 //       $examMeasureInfo["shank_17"] = I("post.shank17");
  //      $examMeasureInfo["shank_18"] = I("post.shank18");
 //       $examMeasureInfo["shank_19"] = I("post.shank19");
  //      $examMeasureInfo["shank_20"] = I("post.shank20");

        D("ExamMeasure")->updateInfo($examMeasureInfo);
        $workorder["exam_measure_id"] = $examMeasureId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examMeasureInfo["mobility"];
        echo json_encode($data);
    }

    public function examBasicCPPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examBasicCPId = I("post.examBasicCPId");

        if($workorder["workorder_type"] == 3){
            if (isNullOrEmpty($examBasicCPId)) {
                $examBasicCPId = D('ExamBasicCpPlus')->add(array(
                    'completed' => -1,
                    'create_time' => getCurrentDate(),
                    'update_time' => getCurrentDate(),
                    'update_id' => $this->customer["customer_id"],
                ));
            }
            $examBasicCPInfo = D("ExamBasicCpPlus")->queryById($examBasicCPId);

            $examBasicCPInfo["update_time"] = getCurrentDate();
            $examBasicCPInfo["update_id"] = $this->customer["customer_id"];
            $examBasicCPInfo["completed"] = I("post.completed");
            for($i = 1; $i < 43; $i++){
                $examBasicCPInfo["a".$i."l"] = I("post.a".$i."l");
                $examBasicCPInfo["a".$i."r"] = I("post.a".$i."r");
            }

            $examBasicCPInfo["a43"] = I("post.a43");
            $examBasicCPInfo["a44"] = I("post.a44");
            $examBasicCPInfo["a45"] = I("post.a45");
            $examBasicCPInfo["a46"] = I("post.a46");
            $examBasicCPInfo["note"] = I("post.note");

            D("ExamBasicCpPlus")->updateInfo($examBasicCPInfo);
            $workorder["exam_basic_cp_id"] = $examBasicCPId;
            D("Workorder")->updateInfo($workorder);
            $data["code"] = 200;
//        $data["alert"] = "workorder".$workorder["exam_basic_cp_id"];
            echo json_encode($data);
        }
        else{
            if (isNullOrEmpty($examBasicCPId)) {
                $examBasicCPId = D('ExamBasicCp')->add(array(
                    'completed' => -1,
                    'create_time' => getCurrentDate(),
                    'update_time' => getCurrentDate(),
                    'update_id' => $this->customer["customer_id"],
                ));
            }
            $examBasicCPInfo = D("ExamBasicCp")->queryById($examBasicCPId);

            $examBasicCPInfo["update_time"] = getCurrentDate();
            $examBasicCPInfo["update_id"] = $this->customer["customer_id"];
            $examBasicCPInfo["completed"] = I("post.completed");
            $examBasicCPInfo["version"] = 2;

            $examBasicCPInfo["ms01"] = I("post.ms01");
            $examBasicCPInfo["ms02"] = I("post.ms02");
            $examBasicCPInfo["ms03"] = I("post.ms03");
            $examBasicCPInfo["ms04"] = I("post.ms04");
            $examBasicCPInfo["ms05"] = I("post.ms05");
            $examBasicCPInfo["ms06"] = I("post.ms06");
            $examBasicCPInfo["ms07"] = I("post.ms07");
            $examBasicCPInfo["ms08"] = I("post.ms08");
            $examBasicCPInfo["ms09"] = I("post.ms09");
            $examBasicCPInfo["ms10"] = I("post.ms10");
            $examBasicCPInfo["mt01"] = I("post.mt01");
            $examBasicCPInfo["mt02"] = I("post.mt02");
            $examBasicCPInfo["mt03"] = I("post.mt03");
            $examBasicCPInfo["mt04"] = I("post.mt04");
            $examBasicCPInfo["mt05"] = I("post.mt05");
            $examBasicCPInfo["mt06"] = I("post.mt06");
            $examBasicCPInfo["mt07"] = I("post.mt07");
            $examBasicCPInfo["mt08"] = I("post.mt08");
            $examBasicCPInfo["mt09"] = I("post.mt09");
            $examBasicCPInfo["mt10"] = I("post.mt10");
            $examBasicCPInfo["mt11"] = I("post.mt11");
            $examBasicCPInfo["mt12"] = I("post.mt12");
            $examBasicCPInfo["mt13"] = I("post.mt13");
            $examBasicCPInfo["mt14"] = I("post.mt14");
            $examBasicCPInfo["jr01"] = I("post.jr01");
            $examBasicCPInfo["jr02"] = I("post.jr02");
            $examBasicCPInfo["jr03"] = I("post.jr03");
            $examBasicCPInfo["jr04"] = I("post.jr04");
            $examBasicCPInfo["jr05"] = I("post.jr05");
            $examBasicCPInfo["jr06"] = I("post.jr06");
            $examBasicCPInfo["jr07"] = I("post.jr07");
            $examBasicCPInfo["jr08"] = I("post.jr08");
            $examBasicCPInfo["jr09"] = I("post.jr09");
            $examBasicCPInfo["jr10"] = I("post.jr10");
            $examBasicCPInfo["jr11"] = I("post.jr11");
            $examBasicCPInfo["jr12"] = I("post.jr12");
            $examBasicCPInfo["sk01"] = I("post.sk01");
            $examBasicCPInfo["sk02"] = I("post.sk02");
            $examBasicCPInfo["fp01"] = I("post.fp01");
            $examBasicCPInfo["b01"] = I("post.b01");
            $examBasicCPInfo["b02"] = I("post.b02");
            $examBasicCPInfo["b03"] = I("post.b03");
            $examBasicCPInfo["b04"] = I("post.b04");

            D("ExamBasicCp")->updateInfo($examBasicCPInfo);
            $workorder["exam_basic_cp_id"] = $examBasicCPId;
            D("Workorder")->updateInfo($workorder);
            $data["code"] = 200;
//        $data["alert"] = "workorder".$workorder["exam_basic_cp_id"];
            echo json_encode($data);
        }
    }

    public function examBasicScoPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examBasicId = I("post.examBasicId");
        if (isNullOrEmpty($examBasicId)) {
            $examBasicId = D('ExamBasicCommon')->add(array(
                'completed' => -1,
                'version' => 2,
                'type' => 4,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examBasicInfo = D("ExamBasicCommon")->queryById($examBasicId);

        $examBasicInfo["update_time"] = getCurrentDate();
        $examBasicInfo["update_id"] = $this->customer["customer_id"];
        $examBasicInfo["completed"] = I("post.completed");

        for($i = 0; $i < 14; $i++){
            $examBasicInfo["eb".$i] = I("post.eb".$i);
        }

        $workorder["photo0"] = I("post.photo0");
        $workorder["photo1"] = I("post.photo1");
        $workorder["photo2"] = I("post.photo2");

        D("ExamBasicCommon")->updateInfo($examBasicInfo);
        $workorder["exam_basic_id"] = $examBasicId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "$examBasicId".$examBasicId;
        echo json_encode($data);
    }

    public function examMeasureScoPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examMeasureId = I("post.examMeasureId");
        if (isNullOrEmpty($examMeasureId)) {
            $examMeasureId = D('ExamMeasureCommon')->add(array(
                'completed' => -1,
                'version' => 2,
                'type' => 4,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examMeasureInfo = D("ExamMeasureCommon")->queryById($examMeasureId);

        $examMeasureInfo["update_time"] = getCurrentDate();
        $examMeasureInfo["update_id"] = $this->customer["customer_id"];
        $examMeasureInfo["completed"] = I("post.completed");

        $examMeasureInfo["pose"] = I("post.pose");
        for($i = 0; $i < 39; $i++){
            $examMeasureInfo["em".$i] = I("post.em".$i);
        }
        $examMeasureInfo["other"] = I("post.other");
        $examMeasureInfo["note"] = I("post.note");

        D("ExamMeasureCommon")->updateInfo($examMeasureInfo);
        $workorder["exam_measure_id"] = $examMeasureId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;

        echo json_encode($data);
    }

    public function examBasicKafoPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examBasicId = I("post.examBasicId");
        if (isNullOrEmpty($examBasicId)) {
            $examBasicId = D('ExamBasicCommon')->add(array(
                'completed' => -1,
                'version' => 2,
                'type' => $workorder["workorder_type"],
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examBasicInfo = D("ExamBasicCommon")->queryById($examBasicId);

        $examBasicInfo["update_time"] = getCurrentDate();
        $examBasicInfo["update_id"] = $this->customer["customer_id"];
        $examBasicInfo["completed"] = I("post.completed");

        $examBasicInfo["side"] = I("post.side");
        $workorder["side"] = I("post.side");
        for($i = 0; $i <= 41; $i++){
            $examBasicInfo["eb".$i] = I("post.eb".$i);
        }

        $examBasicInfo["ebl0"] = I("post.ebl0");

        D("ExamBasicCommon")->updateInfo($examBasicInfo);
        $workorder["exam_basic_id"] = $examBasicId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = $examBasicId;
        echo json_encode($data);
    }

    public function examMeasureKafoPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examMeasureId = I("post.examMeasureId");
        if (isNullOrEmpty($examMeasureId)) {
            $examMeasureId = D('ExamMeasureCommon')->add(array(
                'completed' => -1,
                'version' => 2,
                'type' => $workorder["workorder_type"],
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examMeasureInfo = D("ExamMeasureCommon")->queryById($examMeasureId);

        $examMeasureInfo["update_time"] = getCurrentDate();
        $examMeasureInfo["update_id"] = $this->customer["customer_id"];
        $examMeasureInfo["completed"] = I("post.completed");

        $examMeasureInfo["burden"] = I("post.burden");
        for($i = 0; $i <= 24; $i++){
            $examMeasureInfo["em".$i] = I("post.em".$i);
        }

        D("ExamMeasureCommon")->updateInfo($examMeasureInfo);
        $workorder["exam_measure_id"] = $examMeasureId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;

        echo json_encode($data);
    }

    public function examBasicFo()
    {
        $examBasicId = $_REQUEST["examBasicId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examBasicId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examBasicId = $workorder["exam_basic_id"];
        }
        $examBasic = D("ExamBasicCommon")->queryById($examBasicId);

        $employee = D("admin")->where(array('employee_id'=>$examBasic["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_basic_common', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examBasic", $examBasic);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examBasicId", $examBasicId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examBasicFo");
    }

    public function examBasicFoPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examBasicId = I("post.examBasicId");
        if (isNullOrEmpty($examBasicId)) {
            $examBasicId = D('ExamBasicCommon')->add(array(
                'completed' => -1,
                'version' => 2,
                'type' => $workorder["workorder_type"],
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examBasicInfo = D("ExamBasicCommon")->queryById($examBasicId);

        $examBasicInfo["update_time"] = getCurrentDate();
        $examBasicInfo["update_id"] = $this->customer["customer_id"];
        $examBasicInfo["completed"] = I("post.completed");

        $examBasicInfo["side"] = I("post.side");
        $workorder["side"] = I("post.side");
        for($i = 0; $i <= 15; $i++){
            $examBasicInfo["eb".$i] = I("post.eb".$i);
        }

        $examBasicInfo["ebl0"] = I("post.ebl0");

        D("ExamBasicCommon")->updateInfo($examBasicInfo);
        $workorder["exam_basic_id"] = $examBasicId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = $examBasicId;
        echo json_encode($data);
    }

    public function examMeasureFo()
    {

        $examMeasureId = $_REQUEST["examMeasureId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examMeasureId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examMeasureId = $workorder["exam_measure_id"];
        }

        $examMeasure = D("ExamMeasureCommon")->queryById($examMeasureId);
        $employee = D("admin")->where(array('employee_id'=>$examMeasure["update_id"]))->find();
        $map['class_name'] = array('like', array('exam_measure', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $this->assign("role", $user["role"]);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examMeasure", $examMeasure);
        $this->assign("employeeName", $employee["admin_name"]);
        $this->assign("examMeasureId", $examMeasureId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examMeasureFo");
    }

    public function examMeasureFoPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examMeasureId = I("post.examMeasureId");
        if (isNullOrEmpty($examMeasureId)) {
            $examMeasureId = D('ExamMeasureCommon')->add(array(
                'completed' => -1,
                'version' => 2,
                'type' => $workorder["workorder_type"],
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examMeasureInfo = D("ExamMeasureCommon")->queryById($examMeasureId);

        $examMeasureInfo["update_time"] = getCurrentDate();
        $examMeasureInfo["update_id"] = $this->customer["customer_id"];
        $examMeasureInfo["completed"] = I("post.completed");

        $examMeasureInfo["burden"] = I("post.burden");
        for($i = 0; $i <= 3; $i++){
            $examMeasureInfo["em".$i] = I("post.em".$i);
        }

        D("ExamMeasureCommon")->updateInfo($examMeasureInfo);
        $workorder["exam_measure_id"] = $examMeasureId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;

        echo json_encode($data);
    }

    public function examAnalysisPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examAnalysisId = I("post.examAnalysisId");
        if (isNullOrEmpty($examAnalysisId)) {
            $examAnalysisId = D('ExamAnalysis')->add(array(
                'completed' => -1,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examAnalysisInfo = D("ExamAnalysis")->queryById($examAnalysisId);

        $examAnalysisInfo["update_time"] = getCurrentDate();
        $examAnalysisInfo["update_id"] = $this->customer["customer_id"];
        $examAnalysisInfo["completed"] = I("post.completed");
        $examAnalysisInfo["shank_control"] = I("post.shankControl");
        $examAnalysisInfo["shankcontrol_a"] = I("post.shankcontrolA");
        $examAnalysisInfo["shankcontrol_a_inside"] = I("post.shankcontrolAInside");
        $examAnalysisInfo["shankcontrol_b"] = I("post.shankcontrolB");
        $examAnalysisInfo["shankcontrol_c"] = I("post.shankcontrolC");
        $examAnalysisInfo["ankle_type"] = I("post.ankleType");
        $examAnalysisInfo["ankle_type_other"] = I("post.ankleTypeOther");
        $examAnalysisInfo["ankle_size"] = I("post.ankleSize");
        $examAnalysisInfo["ankle_activity"] = I("post.ankleActivity");
        $examAnalysisInfo["ankle_activity_option"] = I("post.ankleActivityOption");
        $examAnalysisInfo["ankle_activity_other"] = I("post.ankleActivityOther");

        D("ExamAnalysis")->updateInfo($examAnalysisInfo);
        $workorder["exam_analysis_id"] = $examAnalysisId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "shankcontrol_c".$examAnalysisInfo["shankcontrol_c"];
        echo json_encode($data);
    }

    public function examBasicSpinePost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examBasicSpineId = I("post.examBasicSpineId");
        if (isNullOrEmpty($examBasicSpineId)) {
            $examBasicSpineId = D('ExamBasicSpine')->add(array(
                'completed' => -1,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examBasicSpineInfo = D("ExamBasicSpine")->queryById($examBasicSpineId);

        $examBasicSpineInfo["update_time"] = getCurrentDate();
        $examBasicSpineInfo["update_id"] = $this->customer["customer_id"];
        $examBasicSpineInfo["completed"] = I("post.completed");
        $examBasicSpineInfo["mobility"] = I("post.mobility");
        $examBasicSpineInfo["stand"] = I("post.stand");
        $examBasicSpineInfo["stand_describe"] = I("post.standDescribe");
        $examBasicSpineInfo["sit"] = I("post.sit");
        $examBasicSpineInfo["sit_describe"] = I("post.sitDescribe");
        $examBasicSpineInfo["endurance"] = I("post.endurance");
        $examBasicSpineInfo["endurance_time"] = I("post.enduranceTime");
        $examBasicSpineInfo["structure"] = I("post.structure");
        $examBasicSpineInfo["broken"] = I("post.broken");
        $examBasicSpineInfo["broken_part"] = I("post.brokenPart");
        $examBasicSpineInfo["bone_quality"] = I("post.boneQuality");
        $examBasicSpineInfo["spine_gap"] = I("post.spineGap");
        $examBasicSpineInfo["feeling"] = I("post.feeling");
        $examBasicSpineInfo["nofeeling"] = I("post.nofeeling");
        $examBasicSpineInfo["nofeeling_part"] = I("post.nofeelingPart");
        $examBasicSpineInfo["pain"] = I("post.pain");
        $examBasicSpineInfo["pain_part"] = I("post.painPart");
        $workorder["photo0"] = I("post.photo0");
        $workorder["photo1"] = I("post.photo1");
        $workorder["photo2"] = I("post.photo2");

        D("ExamBasicSpine")->updateInfo($examBasicSpineInfo);
        $workorder["exam_basic_spine_id"] = $examBasicSpineId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "$examBasicId".$examBasicId;
        echo json_encode($data);
    }

    public function examMeasureSpinePost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examMeasureSpineId = I("post.examMeasureSpineId");
        if (isNullOrEmpty($examMeasureSpineId)) {
            $examMeasureSpineId = D('ExamMeasureSpine')->add(array(
                'completed' => -1,
                'version' => 2,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examMeasureSpineInfo = D("ExamMeasureSpine")->queryById($examMeasureSpineId);
//        $examMeasureSpineInfo["version"] = 2;
        $examMeasureSpineInfo["update_time"] = getCurrentDate();
        $examMeasureSpineInfo["update_id"] = $this->customer["customer_id"];
        $examMeasureSpineInfo["completed"] = I("post.completed");
        for($i = 0; $i < 21; $i++){
            $examMeasureSpineInfo["spine".$i] = I("post.spine".$i);
        }
        $examMeasureSpineInfo["pose"] = I("post.pose");
//        $examMeasureSpineInfo["abdomen"] = I("post.abdomen");
//        $examMeasureSpineInfo["oxter"] = I("post.oxter");
//        $examMeasureSpineInfo["xiphoid"] = I("post.xiphoid");
//        $examMeasureSpineInfo["iliopsoas"] = I("post.iliopsoas");
//        $examMeasureSpineInfo["anterior"] = I("post.anterior");
//        $examMeasureSpineInfo["trochanter"] = I("post.trochanter");
//        $examMeasureSpineInfo["vertex"] = I("post.vertex");
//        $examMeasureSpineInfo["sit_height"] = I("post.sitHeight");
//        $examMeasureSpineInfo["stand_height"] = I("post.standHeight");
//        $examMeasureSpineInfo["pelvic"] = I("post.pelvic");
//        $examMeasureSpineInfo["shoulder"] = I("post.shoulder");
//        $examMeasureSpineInfo["cobb"] = I("post.cobb");
//        $examMeasureSpineInfo["rotate"] = I("post.rotate");


        D("ExamMeasureSpine")->updateInfo($examMeasureSpineInfo);
        $workorder["exam_measure_spine_id"] = $examMeasureSpineId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examMeasureSpineInfo["mobility"];
        echo json_encode($data);
    }

    public function examMeasureSpineDrawPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examMeasureSpineId = I("post.examMeasureSpineId");
        if (isNullOrEmpty($examMeasureSpineId)) {
            $examMeasureSpineId = D('ExamMeasureSpine')->add(array(
                'version' => 2,
                'completed2' => -1,
                'create_time' => getCurrentDate(),
                'update_time2' => getCurrentDate(),
                'update_id2' => $this->customer["customer_id"],
            ));
        }
        $examMeasureSpineInfo = D("ExamMeasureSpine")->queryById($examMeasureSpineId);
//        $examMeasureSpineInfo["version"] = 2;
        $examMeasureSpineInfo["update_time2"] = getCurrentDate();
        $examMeasureSpineInfo["update_id2"] = $this->customer["customer_id"];
        $examMeasureSpineInfo["completed2"] = I("post.completed");
        $examMeasureSpineInfo["chest1"] = I("post.chest1");
        $examMeasureSpineInfo["chest2"] = I("post.chest2");
        $examMeasureSpineInfo["chest3"] = I("post.chest3");
        $examMeasureSpineInfo["chest4"] = I("post.chest4");

        D("ExamMeasureSpine")->updateInfo($examMeasureSpineInfo);
        $workorder["exam_measure_spine_id"] = $examMeasureSpineId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examMeasureSpineInfo["mobility"];
        echo json_encode($data);
    }


/*
    public function createOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $workorder["sales_id"] = I("post.salesId");
        $workorder["update_time"] = getCurrentDate();
//        $workorder["examinator_id"] = $this->customer["customer_id"];
//        $workorder["patient_id"] = I("post.patientId");
//        $workorder["exam_basic_id"] = I("post.examBasicId");
//        $workorder["exam_measure_id"] = I("post.examMeasureId");
//        $workorder["status"] = 1;

        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $processor = D("Employee")->queryById($this->customer["customer_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',', $singleEmployee["job"]);
            if ($job[0] == "3" || $job[0] == "100") {//客服
                array_push($openidArray, $singleEmployee["openid"]);
            }
            if ($singleEmployee["employee_id"] == $workorder["sales_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $workorder["workorder_id"];
            $notifyData["first"] = "收到新的工单：" . $workorder["workorder_no"];
            $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
            $notifyData["keyword2"] = $workorder["update_time"];
            $notifyData["keyword3"] = $processor["admin_name"];
            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["admin_name"];
            WorkorderCreateNotify($notifyData);
        }

        echo json_encode($data);
    }
*/
    public function createOrder()
    {
        $data["code"] = 200;
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
//        $workorder["sales_id"] = I("post.salesId");
        $workorder["update_time"] = getCurrentDate();
//        $workorder["examinator_id"] = $this->customer["customer_id"];
//        $workorder["patient_id"] = I("post.patientId");
//        $workorder["exam_basic_id"] = I("post.examBasicId");
//        $workorder["exam_measure_id"] = I("post.examMeasureId");
        if(I("post.salesId") == -1){
            $workorder["sales_id"] = $workorder["creator_id"];
        }else{
            $workorder["sales_id"] = I("post.salesId");
        }

        if(I("post.examinatorId") == -1){
            $workorder["examinator_id"] = $workorder["creator_id"];
        }else{
            $workorder["examinator_id"] = I("post.examinatorId");
        }

        if(I("post.prescriberId") == -1){
            $workorder["prescriber_id"] = $workorder["creator_id"];
        }else{
            $workorder["prescriber_id"] = I("post.prescriberId");
        }
        $workorder["dispatch_time"] = getCurrentDate();//new
        $workorder["exam_time"] = getCurrentDate();
        $workorder["take_time"] = getCurrentDate();
        if($workorder["workorder_type"] == 3){
            $workorder["status"] = 4;//new
        }else{
//            $workorder["status"] = 1;//new
            $workorder["status"] = 1;//new
        }
        $workorder["type_name"] = I("post.typeName");
        $workorder["price"] = I("post.price");
        $workorder["deposit"] = I("post.deposit");
//        $workorder["batch_no"] = I("post.batchNo");
        $workorder["client_info_id"] = I("post.client_info_id");
        $workorder["payment_status"] = I("post.paymentStatus");
        $workorder["adapt"] = I("post.adapt");
        $workorder["receive_name"] = I("post.receiveName");
        $workorder["receive_contact"] = I("post.receiveContact");
        $workorder["receive_address"] = I("post.receiveAddress");
        $workorder["delivery_date"] = I("post.deliveryDate");
        $workorder["type_name"] = I("post.typeName");
        $processId = D("Process")->add(array(
            'status' => '0,0,0,0,0,0,0,0,0,0,0,0,0,0',
            'version' => 2,
            'update_time' => getCurrentDate(),
        ));
        $workorder["process_id"] = $processId;
        $workorder["process_status"] = '0,0,0,0,0,0,0,0,0,0,0,0,0,0';

        D("Workorder")->updateInfo($workorder);

        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $sales = D("admin")->where(array('employee_id'=>$workorder["sales_id"]))->find();
        $prescriber = D("admin")->where(array('employee_id'=>$workorder["prescriber_id"]))->find();
        $creator = D("admin")->where(array('employee_id'=>$workorder["creator_id"]))->find();

        if(I("post.duplicate") == 1){

            $duplicateExamBasicInfo["create_time"] = getCurrentDate();
            $duplicateExamBasicInfo["update_time"] = getCurrentDate();
            if($duplicateExamBasicInfo["side"] == "左侧"){
                $duplicateExamBasicInfo["side"] = "右侧";
            }
            if($duplicateExamBasicInfo["side"] == "右侧"){
                $duplicateExamBasicInfo["side"] = "左侧";
            }
            $duplicateExamMeasureInfo["create_time"] = getCurrentDate();
            $duplicateExamMeasureInfo["update_time"] = getCurrentDate();

            $duplicateExamBasicCpInfo["create_time"] = getCurrentDate();
            $duplicateExamBasicCpInfo["update_time"] = getCurrentDate();

            if($workorder["workorder_type"] == 1){
                $duplicateExamBasicInfo = D("ExamBasic")->queryById($workorder["exam_basic_id"]);
                $duplicateExamBasicInfo["exam_basic_id"] = null;
                $duplicateExamBasicId = D('ExamBasic')->add($duplicateExamBasicInfo);

                $duplicateExamMeasureInfo = D("ExamMeasure")->queryById($workorder["exam_measure_id"]);
                $duplicateExamMeasureInfo["exam_measure_id"] = null;
                $duplicateExamMeasureId = D('ExamMeasure')->add($duplicateExamMeasureInfo);

                $duplicateExamBasicCpInfo = D("ExamBasicCp")->queryById($workorder["exam_basic_cp_id"]);
                $duplicateExamBasicCpInfo["exam_basic_cp_id"] = null;
                $duplicateExamBasicCpId = D('ExamBasicCp')->add($duplicateExamBasicCpInfo);
            }else{
                $duplicateExamBasicInfo = D("ExamBasicCommon")->queryById($workorder["exam_basic_id"]);
                $duplicateExamBasicInfo["exam_basic_common_id"] = null;
                $duplicateExamBasicId = D('ExamBasicCommon')->add($duplicateExamBasicInfo);

                $duplicateExamMeasureInfo = D("ExamMeasureCommon")->queryById($workorder["exam_measure_id"]);
                $duplicateExamMeasureInfo["exam_measure_common_id"] = null;
                $duplicateExamMeasureId = D('ExamMeasureCommon')->add($duplicateExamMeasureInfo);
            }

            $duplicateWorkorder = $workorder;
            $duplicateWorkorder["workorder_id"] = null;
            $enterprise = D("Enterprise")->where(array('enterprise_code'=>$user["enterprise_code"]))->find();

            switch ($workorder["workorder_type"]) {
                case 1:
                    $name = $enterprise["workorder_no_head"] . "AFO-";
                    break;
                case 5:
                    $name = $enterprise["workorder_no_head"] . "(H)KAFO-";
                    break;
                case 6:
                    $name = $enterprise["workorder_no_head"] . "KO-";
                    break;
                case 7:
                    $name = $enterprise["workorder_no_head"] . "FO-";
                    break;
                default:
                    $name = $enterprise["workorder_no_head"] . "-";
                    break;
            }

//            $duplicateWorkorder["workorder_no"] = $enterprise["workorder_no_head"] . "AFO-".date('Ymd',time()) . sprintf("%03d", substr(D("Workorder")->count(), -3, 3)+1);
            $duplicateWorkorder["workorder_no"] = $name.date('Ymd',time()) . sprintf("%03d", substr(D("Workorder")->count(), -3, 3)+1);
            $duplicateWorkorder["status"] = -1;
            $duplicateWorkorder["create_time"] = getCurrentDate();
            $duplicateWorkorder["update_time"] = getCurrentDate();
            $duplicateWorkorder["process_id"] = null;
            $duplicateWorkorder["process_status"] = "0,0,0,0,0,0,0,0,0,0,0,0,0,0";
            $duplicateWorkorder["batch_no"] = null;
            $duplicateWorkorder["rx_id"] = null;
            if($duplicateWorkorder["side"] == "左侧"){
                $duplicateWorkorder["side"] = "右侧";
            }else{
                $duplicateWorkorder["side"] = "左侧";
            }
            $duplicateWorkorder["exam_basic_id"] = $duplicateExamBasicId;
            $duplicateWorkorder["exam_measure_id"] = $duplicateExamMeasureId;
            $duplicateWorkorder["exam_basic_cp_id"] = $duplicateExamBasicCpId;
            $duplicateWorkorder["review_id"] = null;
            $duplicateWorkorder["review_status"] = null;
            $duplicateWorkorder["audit_id"] = null;
            $duplicateWorkorder["audit_status"] = null;
            $duplicateWorkorder["auditor_id"] = null;

            $duplicateWorkorderId = D('Workorder')->add($duplicateWorkorder);

            $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $duplicateWorkorderId;
            $notifyData["first"] = "复制工单“".$duplicateWorkorder["workorder_no"]."”已创建。";
            $notifyData["keyword1"] = "患者：" . $patient["patient_name"] . " ".$duplicateWorkorder["side"]."，" . $patient["sex"] . "，" . $patient["age"] . "岁。[诊断--" . $patient["diagnosis"] . "]。";
            $notifyData["keyword2"] = getCurrentDate();
            $notifyData["keyword3"] = $creator["admin_name"]."，请点击进入工单详情进行提交。";
            $notifyData["remark"] = "下单人：" . $creator["admin_name"] . "，业务员：" . $sales["admin_name"] . "。处方人：" . $prescriber["admin_name"];
            $notifyData["openid"] = $creator["openid"];
            WorkorderProcessingNotify($notifyData);
        }

        $data["code"] = 200;
        //检查表不推送消息
        if($workorder["workorder_type"] == 3) {
            //推送信息到接单人
            $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $workorder["workorder_id"];
            $notifyData["first"] = "已提交新的物理检查表：" . $workorder["workorder_no"];
            $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
            $notifyData["keyword2"] = $workorder["update_time"];
            $notifyData["keyword3"] = $user["admin_name"];
            $notifyData["remark"] = "患者：" . $patient["patient_name"] . " ".$workorder["side"]."，" . $patient["sex"] . "，" . $patient["age"] . "岁。 下单人：" . $creator["admin_name"] . "业务员：" . $sales["admin_name"];

//            $adminRoleArr = array("1");
//            $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
            $openidArray = getNotifyArrByName("createWorkorderCp", $user["enterprise_code"]);
            array_push($openidArray, $user["openid"]);
            $openidArray = array_unique($openidArray);
            foreach ($openidArray as $openid) {
                $notifyData["openid"] = $openid;
                WorkorderCreateNotify($notifyData);
            }

            echo json_encode($data);
            return;
        }
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $workorder["workorder_id"];
        $notifyData["first"] = "收到新的工单：" . $workorder["workorder_no"] . "。请 ".$prescriber["admin_name"]." 登录处方系统出处方。";
        $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
        $notifyData["keyword2"] = $workorder["update_time"];
        $notifyData["keyword3"] = $user["admin_name"];
        $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . " ".$workorder["side"]."，" . $patient["sex"] . "，" . $patient["age"] . "岁。 下单人：" . $creator["admin_name"] . "业务员：" . $sales["admin_name"];

//        $adminRoleArr = array("1");
//        $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
        $openidArray = getNotifyArrByName("createWorkorder", $user["enterprise_code"]);
        array_push($openidArray, $user["openid"]);
        array_push($openidArray, $prescriber["openid"]);
        $openidArray = array_unique($openidArray);
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            WorkorderCreateNotify($notifyData);
        }

        echo json_encode($data);
    }
//    //略过此功能
//    public function dispatchOrder()
//    {
//        $workorder = D("Workorder")->queryById(I("post.workorderId"));
//        $workorder["dispatch_time"] = getCurrentDate();
//
//        $workorder["status"] = 2;
//
//        D("Workorder")->updateInfo($workorder);
//        $data["code"] = 200;
////        $data["alert"] = "mobility".$examBasicInfo["mobility"];
//
//        //推送信息到接单人
//        $processor = D("Employee")->queryById($this->customer["customer_id"]);
//        $sales = D("Employee")->queryById($workorder["sales_id"]);
//        $patient = D("Patient")->queryById($workorder["patient_id"]);
//        $employeeList = D("Employee")->alias("e")
//            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
//            ->field("e.*, c.openid")
//            ->select();
//        $openidArray = array();
//        foreach ($employeeList as $singleEmployee) {
//            if (in_array($singleEmployee["openid"], $openidArray)) {
//                continue;
//            }
//            $job = explode(',', $singleEmployee["job"]);
//            if ($job[0] == "4" || $job[0] == "100") {//临床部协调人
//                array_push($openidArray, $singleEmployee["openid"]);
//            }
////            if ($singleEmployee["employee_id"] == $workorder["sales_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
////                array_push($openidArray, $singleEmployee["openid"]);
////            }
//        }
//        foreach ($openidArray as $openid) {
//            $notifyData["openid"] = $openid;
//            $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $workorder["workorder_id"];
//            $notifyData["first"] = "收到新的工单：" . $workorder["workorder_no"] . "，请指派检测评估矫形师。";
//            $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
//            $notifyData["keyword2"] = $workorder["dispatch_time"];
//            $notifyData["keyword3"] = $processor["admin_name"];
//            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["admin_name"];
//            WorkorderCreateNotify($notifyData);
//        }
//
//        echo json_encode($data);
//    }
//    //略过此功能
//    public function takeOrder()
//    {
//        $workorder = D("Workorder")->queryById(I("post.workorderId"));
//        $workorder["take_time"] = getCurrentDate();
//
//        if(I("post.examinatorId") == -1){
//            $workorder["exam_time"] = $workorder["update_time"];
//            $workorder["examinator_id"] = $workorder["creator_id"];
//        }else{
//            $workorder["examinator_id"] = I("post.examinatorId");
//        }
//
//        if(I("post.prescriberId") == -1){
//            $workorder["prescriber_id"] = $workorder["creator_id"];
//        }else{
//            $workorder["prescriber_id"] = I("post.prescriberId");
//        }
//        $workorder["batch_no"] = I("post.batchNo");
//
//        D("Workorder")->updateInfo($workorder);
//        $data["code"] = 200;
////        $data["alert"] = "examinator_id".$workorder["examinator_id"];
//
//        if(I("post.examinatorId") != -1) {
//            //推送信息到接单人
//            $processor = D("Employee")->queryById($this->customer["customer_id"]);
//            $sales = D("Employee")->queryById($workorder["sales_id"]);
//            $patient = D("Patient")->queryById($workorder["patient_id"]);
//            $employeeList = D("Employee")->alias("e")
//                ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
//                ->field("e.*, c.openid")
//                ->select();
//            $openidArray = array();
//            foreach ($employeeList as $singleEmployee) {
//                if (in_array($singleEmployee["openid"], $openidArray)) {
//                    continue;
//                }
////            $job = explode(',', $singleEmployee["job"]);
////            if ($job[1] == "2") {
////                array_push($openidArray, $singleEmployee["openid"]);
////            }
//                if ($singleEmployee["employee_id"] == $workorder["examinator_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
//                    array_push($openidArray, $singleEmployee["openid"]);
//                }
//            }
//            foreach ($openidArray as $openid) {
//                $notifyData["openid"] = $openid;
//                $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $workorder["workorder_id"];
//                $notifyData["first"] = "收到新的工单：" . $workorder["workorder_no"] . "，请填写检测评估数据。";
//                $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
//                $notifyData["keyword2"] = $workorder["take_time"];
//                $notifyData["keyword3"] = $processor["admin_name"];
//                $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["admin_name"];
//                WorkorderCreateNotify($notifyData);
//            }
//        }
//        echo json_encode($data);
//    }
//    //略过此功能
//    public function examOrder()
//    {
//        $workorder = D("Workorder")->queryById(I("post.workorderId"));
//        $workorder["exam_time"] = getCurrentDate();
//
//
//        D("Workorder")->updateInfo($workorder);
//        $data["code"] = 200;
////        $data["alert"] = "mobility".$examBasicInfo["mobility"];
//
//        //推送信息到接单人
//        $processor = D("Employee")->queryById($this->customer["customer_id"]);
//        $sales = D("Employee")->queryById($workorder["sales_id"]);
//        $patient = D("Patient")->queryById($workorder["patient_id"]);
//        $employeeList = D("Employee")->alias("e")
//            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
//            ->field("e.*, c.openid")
//            ->select();
//        $openidArray = array();
//        foreach ($employeeList as $singleEmployee) {
//            if (in_array($singleEmployee["openid"], $openidArray)) {
//                continue;
//            }
//            $job = explode(',', $singleEmployee["job"]);
//            if ($job[0] == "4" || $job[0] == "100") {//临床部协调人
//                array_push($openidArray, $singleEmployee["openid"]);
//            }
////            if ($singleEmployee["employee_id"] == $workorder["sales_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
////                array_push($openidArray, $singleEmployee["openid"]);
////            }
//        }
//        foreach ($openidArray as $openid) {
//            $notifyData["openid"] = $openid;
//            $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $workorder["workorder_id"];
//            $notifyData["first"] = "工单：" . $workorder["workorder_no"] . " 检测评估信息已提交。";
//            $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
//            $notifyData["keyword2"] = $workorder["exam_time"] . " - " . $processor["admin_name"];
//            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["admin_name"];
//            WorkorderChangeNotify($notifyData);
//        }
//
//        echo json_encode($data);
//    }
    public function submitOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));

        $workorder["submit_time"] = getCurrentDate();
        $workorder["type_name"] = I("post.typeName");
        $workorder["deposit"] = I("post.deposit");
        $workorder["price"] = I("post.price");
        $workorder["delivery_date"] = I("post.deliveryDate");
        $workorder["adapt"] = I("post.adapt");
        $workorder["status"] = 3;
        $workorder["batch_no"] = I("post.batchNo");
        $prescriberChanged = -1;
        if($workorder["prescriber_id"] != I("post.prescriberId")){
            $prescriberChanged = 1;
            if(I("post.prescriberId") == -1){
                $workorder["prescriber_id"] = $workorder["creator_id"];
            }else{
                $workorder["prescriber_id"] = I("post.prescriberId");
            }

        }


        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $sales = D("admin")->where(array('employee_id'=>$workorder["sales_id"]))->find();
        $prescriber = D("admin")->where(array('employee_id'=>$workorder["prescriber_id"]))->find();
        $creator = D("admin")->where(array('employee_id'=>$workorder["creator_id"]))->find();
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();

        $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $workorder["workorder_id"];
        $notifyData["first"] = "工单“".$workorder["workorder_no"]."”已经由 " . $user["admin_name"] . " 接单。矫形器种类：" . $workorder["type_name"] ."，订单价格：" . $workorder["price"] ."，已收预付款：" . $workorder["deposit"] ."，预计交付时间：" . $workorder["delivery_date"];
        $notifyData["keyword1"] = "患者：" . $patient["patient_name"] . " ".$workorder["side"]."，" . $patient["sex"] . "，" . $patient["age"] . "岁。[诊断--" . $patient["diagnosis"] . "]";
        $notifyData["keyword2"] = getCurrentDate();
        $notifyData["keyword3"] = "库管";
        $notifyData["remark"] = "下单人：" . $creator["admin_name"] . "，业务员：" . $sales["admin_name"] . "。处方人：" . $prescriber["admin_name"];

        $adminRoleArr = array("1", "8");
        $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
        array_push($openidArray, $user["openid"]);
        if($prescriberChanged == 1){
            $notifyData["first"]  .=  "。** 处方人变更为：" . $prescriber["admin_name"] ." **";
            array_push($openidArray, $prescriber["openid"]);
        }
        $openidArray = array_unique($openidArray);
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            WorkorderProcessingNotify($notifyData);
        }

        echo json_encode($data);
    }

    public function changeStatus()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));

        $workorder["type_name"] = I("post.typeName");
        $workorder["sales_id"] = I("post.salesId");
        $workorder["examinator_id"] = I("post.examinatorId");
        $workorder["prescriber_id"] = I("post.prescriberId");
        $workorder["creator_id"] = I("post.creatorId");
        $workorder["deposit"] = I("post.deposit");
        $workorder["price"] = I("post.price");
        $workorder["adapt"] = I("post.adapt");
        $workorder["delivery_date"] = I("post.deliveryDate");
        $workorder["adapt"] = I("post.adapt");
        $workorder["status"] = I("post.statusId");

        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $sales = D("admin")->where(array('employee_id'=>$workorder["sales_id"]))->find();
        $prescriber = D("admin")->where(array('employee_id'=>$workorder["prescriber_id"]))->find();
        $creator = D("admin")->where(array('employee_id'=>$workorder["creator_id"]))->find();
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();

        $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $workorder["workorder_id"];
        $notifyData["first"] = "工单“".$workorder["workorder_no"]."”状态已变更。";

        $notifyData["keyword2"] = getCurrentDate();

        $notifyData["remark"] = "患者：" . $patient["patient_name"] . " ".$workorder["side"]."，" . $patient["sex"] . "，" . $patient["age"] . "岁。[诊断--" . $patient["diagnosis"] . "]。下单人：" . $creator["admin_name"] . "，业务员：" . $sales["admin_name"] . "。处方人：" . $prescriber["admin_name"];

        switch ($workorder["status"]) {
            case -2:
                $notifyData["keyword1"] = "状态变更为 [已关闭]";
                $notifyData["keyword3"] = "下单人：". $creator["admin_name"];
//                $adminRoleArr = array("1");
//                $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
                $openidArray = getNotifyArrByName("changeWorkorderClose", $user["enterprise_code"]);
                array_push($openidArray, $creator["openid"]);
                break;
            case -1:
                $notifyData["keyword1"] = "状态变更为 [未提交]";
                $notifyData["keyword3"] = "下单人：". $creator["admin_name"];
//                $adminRoleArr = array("1");
//                $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
                $openidArray = getNotifyArrByName("changeWorkorderNew", $user["enterprise_code"]);
                array_push($openidArray, $creator["openid"]);
                break;
            case 1:
                $notifyData["keyword1"] = "状态变更为 [出处方]";
                $notifyData["keyword3"] = "处方人：". $prescriber["admin_name"];
//                $adminRoleArr = array("1");
//                $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
                $openidArray = getNotifyArrByName("changeWorkorderRx", $user["enterprise_code"]);
                array_push($openidArray, $prescriber["openid"]);
                break;
            case 3:
                $notifyData["keyword1"] = "状态变更为 [制作中]";
                $notifyData["keyword3"] = "领料人、制作人员";
//                $adminRoleArr = array("1","5");
//                $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
                $openidArray = getNotifyArrByName("changeWorkorderProduce", $user["enterprise_code"]);
                break;
            case 4:
                $notifyData["keyword1"] = "状态变更为 [已完成]";
                $notifyData["keyword3"] = "客服";
//                $adminRoleArr = array("3");
//                $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
                $openidArray = getNotifyArrByName("changeWorkorderEnd", $user["enterprise_code"]);
                break;
        }

        $openidArray = array_unique($openidArray);
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            WorkorderProcessingNotify($notifyData);
        }

        echo json_encode($data);
    }

    public function closeOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $workorder["close_reason"] = I("post.closeReason");
        $workorder["close_time"] = getCurrentDate();
        $workorder["status"] = -2;

        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $sales = D("admin")->where(array('employee_id'=>$workorder["sales_id"]))->find();
        $creator = D("admin")->where(array('employee_id'=>$workorder["creator_id"]))->find();
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();

        $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $workorder["workorder_id"];
        $notifyData["first"] = "工单：" . $workorder["workorder_no"] . " 已关闭，关闭原因：" . $workorder["close_reason"];
        $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
        $notifyData["keyword2"] = $workorder["close_time"] . " - " . $user["admin_name"];
        $notifyData["remark"] = "患者：" . $patient["patient_name"] . " ".$workorder["side"]."，" . $patient["sex"] . "，" . $patient["age"] . "岁。 下单人：" . $creator["admin_name"] . "，业务员：" . $sales["admin_name"];

        $adminRoleArr = array("1","3");//是否加库管
        $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
        array_push($openidArray, $user["openid"]);
        array_push($openidArray, $sales["openid"]);
        $openidArray = array_unique($openidArray);
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            WorkorderChangeNotify($notifyData);
        }

        echo json_encode($data);
    }

    public function storageOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $workorder["storage_time"] = getCurrentDate();
        $workorder["batch_no"] = I("post.batchNo");

        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $prescriber = D("admin")->where(array('employee_id'=>$workorder["prescriber_id"]))->find();
        $sales = D("admin")->where(array('employee_id'=>$workorder["sales_id"]))->find();
        $creator = D("admin")->where(array('employee_id'=>$workorder["creator_id"]))->find();
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();

        $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $workorder["workorder_id"];
        $notifyData["first"] = "工单：" . $workorder["workorder_no"] . " 已入库，可以发货。矫形器种类：" . $workorder["type_name"] ."，订单价格：" . $workorder["price"];
        $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
        $notifyData["keyword2"] = $workorder["storage_time"] . " - " . $user["admin_name"];
        $notifyData["remark"] = "患者：" . $patient["patient_name"] . " ".$workorder["side"]."，" . $patient["sex"] . "，" . $patient["age"] . "岁。 下单人：" . $creator["admin_name"] . "，业务员：" . $sales["admin_name"]. "。处方人：" . $prescriber["admin_name"];

        $adminRoleArr = array("1","8");//是否加库管
        $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
        array_push($openidArray, $user["openid"]);
        array_push($openidArray, $sales["openid"]);
        $openidArray = array_unique($openidArray);
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            WorkorderChangeNotify($notifyData);
        }

        echo json_encode($data);
    }
    public function endOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $workorder["batch_no"] = I("post.batchNo");
        $workorder["end_time"] = getCurrentDate();
        $workorder["status"] = 4;

        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $creator = D("admin")->where(array('employee_id'=>$workorder["creator_id"]))->find();
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $prescriber = D("admin")->where(array('employee_id'=>$workorder["prescriber_id"]))->find();
        $sales = D("admin")->where(array('employee_id'=>$workorder["sales_id"]))->find();
        $examinator = D("admin")->where(array('employee_id'=>$workorder["examinator_id"]))->find();
        $user = D("admin")->where(array('employee_id'=>$this->customer["customer_id"]))->find();

        $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/workorder/index/id/" . $workorder["workorder_id"];
        if($workorder["workorder_type"] == 1){
            $notifyData["first"] = "DRAFO工单已交付,工单号：" . $workorder["workorder_no"];
        }
        if($workorder["workorder_type"] == 2){
            $notifyData["first"] = "脊柱工单已交付,工单号：" . $workorder["workorder_no"];
        }
        $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
        $notifyData["keyword2"] = "矫形器种类：" . $workorder["type_name"] ."，订单价格：" . $workorder["price"];
        $notifyData["keyword3"] = $workorder["update_time"] . "（下单时间）";
        $notifyData["keyword4"] = $workorder["end_time"]. "（发货时间）";
        $notifyData["keyword5"] = "下单人：" . $creator["admin_name"] . "，检测人：" . $examinator["admin_name"];
        $notifyData["remark"] = "患者：" . $patient["patient_name"] . " ".$workorder["side"]."，" . $patient["sex"] . "，" . $patient["age"] . "岁。 业务员：" . $sales["admin_name"]. "。处方人：" . $prescriber["admin_name"];

        $adminRoleArr = array("1");//是否加库管
        $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
        array_push($openidArray, $user["openid"]);
        array_push($openidArray, $creator["openid"]);
        $openidArray = array_unique($openidArray);
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            workorderEndNotify($notifyData);
        }

        echo json_encode($data);
    }

}